<?php

class CourseController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','searchByDistrict','create'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete','printCourse', 'search'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 */
	public function actionView()
	{
		$this->render('view',array(
			'model'=>$this->loadModel(),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Course;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Course']))
		{
                   
			$model->attributes=$_POST['Course'];
                        $model->subjectIds = $_POST['Course']['subjectIds'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
                                
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionUpdate()
	{
		$model=$this->loadModel();

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Course']))
		{
			$model->attributes=$_POST['Course'];
                        $model->subjectIds = $_POST['Course']['subjectIds'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
                        
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 */
	public function actionDelete()
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel()->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(array('index'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
//		$dataProvider=new CActiveDataProvider('Course');
//		$this->render('index',array(
//			'dataProvider'=>$dataProvider,
//		));
            $this->layout='//layouts/column1';
            $this->render('tabSearch',array(
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$this->render('admin',array(	
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=Course::model()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='course-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
        public function actionSearchByDistrict() {
            
            $criteria=new CDbCriteria;
		$criteria->compare('id_district',$_GET['id_district']);
                $criteria->compare('status',0);
                $dataProvider=new CActiveDataProvider('Course', array('criteria'=>$criteria));
		$this->renderPartial('_tabView',array(
                        'pagination'=>array(
                            'pageSize'=>20,
                        ),
			'dataProvider'=>$dataProvider
		));
        }
        public function actionPrintCourse() {
            $checkedIDs = $_GET['selectedItems'];
            $criteria=new CDbCriteria;
            if (empty($checkedIDs) === false) {
                    $criteria->compare('id', $checkedIDs, true);
            }
            for ($index = 0; $index < count($checkedIDs); $index++) {
                $course = Course::model()->findByPK($checkedIDs[$index]);
                $course->is_printed = 1;
                $course->save();
            }
            $dataProvider = new CActiveDataProvider('Course', array(
			'criteria'=>$criteria,
		));
            $this->render('print',array(
                'dataProvider'=>$dataProvider,
            ));
        }
        public function actionSearch() {
            $criteria=new CDbCriteria;
                $subject_list = $_POST['subject_list'];
                $district_list = $_POST['district_list'];
                $level_list = $_POST['level_list'];
                $status = $_POST['status'];
                $is_printed = $_POST['is_printed'];
                $id_course = $_POST['id_course'];
                $parent_name = $_POST['parent_name'];
                $cell_phone = $_POST['cell_phone'];
                $table_phone = $_POST['table_phone'];
                $home_number = $_POST['home_number'];
                $street = $_POST['street'];
                $id_district = $_POST['id_district'];
                $limit_date = $_POST['limit_date'];
            if (count($subject_list) > 1) {
                    $criteria->select = "t.id";
                    $criteria->join = ' INNER JOIN  tbl_course_subject cs ON cs.id_course = t.id';
//                    $criteria->join .= ' INNER JOIN  tbl_subject s ON s.id = cs.id_subject';
                    $criteria->compare('cs.id_subject', $subject_list, true);
             }
             $courseList = Course::model()->findAll($criteria);
             
             $criteria1 = new CDbCriteria;
             $id_course_list=array();
           foreach ($courseList as $course) {
               array_push($id_course_list, $course->id);

           }
             $criteria1->compare('id', $id_course_list, true);
             if(count ($district_list) > 1) {
                 $criteria1->compare('id_district', $district_list, true);
             }
             if(count ($level_list) > 1) {
                $criteria1->compare('id_level', $level_list, true);
             }
             
             if($status != 2) {
                 $criteria1->compare('status', $status, true);
             }
             if($is_printed != 2) {
                 $criteria1->compare('is_printed', $is_printed, true);
             }
             $criteria1->compare('id', $id_course, true);
             $criteria1->compare('parent_name', $parent_name, true);
             $criteria1->compare('cell_phone', $cell_phone, true);
             $criteria1->compare('table_phone', $table_phone, true);
             $criteria1->compare('home_number', $home_number, true);
             $criteria1->compare('street', $street, true);
             $criteria1->compare('id_district', $id_district, true);
             $criteria1->compare('limit_date', $limit_date, true);

             $criteria1->order = 'id DESC';
             
           $dataProvider = new CActiveDataProvider('Course', array(
			'criteria'=>$criteria1,
                        'pagination'=>array(
                            'pageSize'=>10,
                    ),
		));
            $this->renderPartial('_result',array(
                'dataProvider'=>$dataProvider,
            ));
        }
}
